/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.freshdirect.src.service;

import com.freshdirect.src.model.Product;
import com.freshdirect.src.dao.ProductDAO;
import com.freshdirect.src.dao.SupplierDAO;
import com.freshdirect.src.model.Supplier;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import static org.apache.commons.lang.StringUtils.isNumeric;
 /*
 * @author Ilias
 */
public class ProductService {
    
    
    public int saveProduct(Product product){
         //Get the Spring Context
        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
         
        //Get the ProductDAO Bean
        ProductDAO productDAO = ctx.getBean("productDAO", ProductDAO.class);
        System.out.println("ProductService -> saveProduct");
        productDAO.setDataSource("admin");
        return productDAO.save(product);
    }

    public List<Product> getProductsbyGroup(String name) {
        //Get the Spring Context
        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");

        //Get the ProductDAO Bean
        ProductDAO productDAO = ctx.getBean("productDAO", ProductDAO.class);

        //Find Group id from name
        int id;
        if(name.equals("fruits")){
           id = 1;
        }
        else if(name.equals("vegetables")){
           id = 2;
        }
        else if(name.equals("seafood")){
           id = 3;
        }
        else if(name.equals("meat")){
           id = 4;
        }
        else if(name.equals("dairy")){
           id = 5;
        }
        else if(name.equals("bakerynpastry")){
            id = 6;
        }
        else if(name.equals("cerealnpalses")){
            id = 7;
        }
        else if(name.equals("frozen")){
            id = 8;
        }
        else if(name.equals("cans")){
            id = 9;
        }
        else if(name.equals("flowers")){
            id = 10;
        }
        else if(name.equals("pet")){
            id = 11;
        }
        else{
            id = 0;
        }
        //Get products from DB
        if(id!=0){
            productDAO.setDataSource("user");
            return productDAO.getProductsbyGroup(id);
        }
        else{
            return new ArrayList<Product>();
        }
    }

    public List<Product> search(Map<String, String> params) {
        String prodIDs;
        String supplierIDs;
        String keywords;
        String sort;
        String groups = "";
        String suppliers = "";

        //Get the Spring Context
        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");

        //Get the ProductDAO Bean
        ProductDAO productDAO = ctx.getBean("productDAO", ProductDAO.class);
        SupplierDAO supplierDAO = ctx.getBean("supplierDAO",SupplierDAO.class);
        
        prodIDs = params.get("categories");
        if(prodIDs == null){
            prodIDs = "";
        }
        if(!prodIDs.equals("")){
            String[] ids = prodIDs.split(",");
            for (String id : ids) {
                if ((isNumeric(id)) && (!id.equals(""))) {
                    groups += id+",";
                }
            }
        }
        else{
            
        }
        
        
        supplierIDs = params.get("suppliers");
        if(supplierIDs == null){
            supplierIDs = "";
        }
        if(!supplierIDs.equals("")){
            String[] ids = supplierIDs.split(",");
            for (String id : ids) {
                if ((isNumeric(id)) && (!id.equals(""))) {
                    suppliers += id+ ",";
                }
            }
        }
        else{
            
        }
        
        keywords =  params.get("keywords");
        if(keywords == null){
            keywords = "";
        }
        
        //Save product to DB
        productDAO.setDataSource("user");
        supplierDAO.setDataSource("user");
        
        if(groups.equals("")){
            groups = productDAO.getAllGroups();
        }
        if(suppliers.equals("")){
            suppliers = supplierDAO.getAllSupplierstoString();
        }
        List<Product> products = productDAO.searchProducts(groups, suppliers, keywords);
        
        sort = params.get("sort");
        if(sort == null){
            sort = "name";
        }
        if (sort.equals("name")){
            Collections.sort(products,Product.NameComparator);
        }
        else if (sort.equals("priceasc")){
            Collections.sort(products,Product.Price);
        }
        else if (sort.equals("pricedsc")){
            Collections.sort(products,Product.Price);
            Collections.sort(products, Collections.reverseOrder());
        }
        else if (sort.equals("supplier")){
            Collections.sort(products,Product.SupplierName);
        }
        return products;
    }

    public Product getProductbyId(int id) {
         //Get the Spring Context
        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");

        //Get the ProductDAO Bean
        ProductDAO productDAO = ctx.getBean("productDAO", ProductDAO.class);
       System.out.println("ProductService -> getProductbyId");
        //Save product to DB
        productDAO.setDataSource("user");
        return productDAO.getById(id);
    }
    
    public List<Product> getAll() {
        //Get the Spring Context
        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");

        //Get the ProductDAO Bean
        ProductDAO productDAO = ctx.getBean("productDAO", ProductDAO.class);
       System.out.println("ProductService -> getProductbyId");
        //Save product to DB
       productDAO.setDataSource("admin");
        return productDAO.getAll();
    }

    public List<Supplier> getAllSuppliers() {
        //Get the Spring Context
        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");

        //Get the ProductDAO Bean
        SupplierDAO supplierDAO = ctx.getBean("supplierDAO", SupplierDAO.class);
       System.out.println("ProductService -> getProductbyId");
        //Save product to DB
       supplierDAO.setDataSource("admin");
        return supplierDAO.getAll();
    }

    public List<Product> getSuggestions(int id) {
         //Get the Spring Context
        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");

        //Get the ProductDAO Bean
        ProductDAO productDAO = ctx.getBean("productDAO", ProductDAO.class);
       System.out.println("ProductService -> getSuggestions");
        //Save product to DB
       productDAO.setDataSource("user");
        return productDAO.getSuggestedbyId(id);
    }

    public void updateQuantity(int id, int n) {
         //Get the Spring Context
        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");

        //Get the ProductDAO Bean
        ProductDAO productDAO = ctx.getBean("productDAO", ProductDAO.class);
        productDAO.setDataSource("admin");
        productDAO.updateQuantity(id,n);
    }

    public int findDegreeOfSeparation(int sup1, int sup2) {
         //Get the Spring Context
        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
         
        //Get the ProductDAO Bean
        ProductDAO productDAO = ctx.getBean("productDAO", ProductDAO.class);
        System.out.println("ProductService -> findDegree");
        productDAO.setDataSource("admin");
        int degree = productDAO.findDegree(sup1,sup2);
        System.out.println(degree);
        return degree;
    }
    

}
